-- MachinePlatform
-- Created by cheny3 Jun/03/2016
-- 战斗平台

-- 初始化
function initMachinePlatform(self)
    local pos = self.gridData:getPos();
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local skillId = self.gridData.buff_skill;
    -- 建筑图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    if skillId == 2205 then
        -- 是第2技能
        iconPath = getLevelItemIconPath(buildingInfo["icon2"]);
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 能否靠近
            if not ActivityDungeonM.isInActivityBossLayer() and not self.gridData:canHit() then
                alert(getLocStr("grid_unreachable"));
                return false;
            end

            local args = DungeonM.queryElement(self.gridData.element, "args");
            local cost = args["cost"];
            local name = ItemM.query(cost[2], "name");

            -- 根据skillId获取dialog
            local dialog = buildingInfo["dialog"];
            if skillId == 2205 then
                -- 是第2个技能的
                dialog = buildingInfo["dialog2"];
            end

            dialog = string.format(dialog, cost[3], name);

            local function closePlatform()
                if MachinePlatformM.enoughForCost(pos) then
                    alert(buildingInfo["bonus_tip"]);
                    -- 关闭作战平台
                    DungeonActionM.go("close_platform", pos);
                else
                    alert(string.format(getLocStr("something_not_enough"), name));
                end
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 音效
            AudioM.playFx("ui_open1");

            -- 打开建筑查看界面
            self:openBuildingView(closePlatform, 0, dialog, nil, nil, iconPath);
        end
    end

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;
end
